package servidor;
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;

class Server {

	private ServerSocket server;
	private Socket socket;
	public InputStream in;
	public OutputStream out;

	public static void main (String args[]){

		Server servidor = new Server(8001);
	}
	public Server (int port) {

		try {
			server = new ServerSocket (port);
			System.out.println ("Servidor Java Activo!" + "\n");
			System.out.println(""+server+"\n");
			int i = 0;
			socket = server.accept();
			in = socket.getInputStream();
			out = socket.getOutputStream();
			while(socket.getRemoteSocketAddress()!=null){
				System.out.println("Esta conectado ...");
				BufferedImage imagen = ImageIO.read(in);
				i++;
				String ext = "png";
				String nombre = "Prueba "+i+"."+ext;
				if(imagen != null)
				{
					ImageIO.write(imagen, ext , new File(nombre));
					System.out.println("Obtuve " + nombre);
					imagen.flush();
				}
				try{
					EchoServe (socket);
				}catch(IOException ioe){
					socket.close();
					in.close();
					out.close();
				}
			}
		socket.close();
		in.close();
		out.close();
		}
		catch (Exception e) {
			e.printStackTrace();

		}
	}
	public void EchoServe (Socket s) 
	throws IOException {
		socket = s;
		System.out.println("Serving: "+ socket); 
		BufferedReader in = new BufferedReader(	new InputStreamReader(socket.getInputStream()));
		// Enable auto-flush:
		PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
	}
}